<?php
namespace Clover\Controller;

use Think\Controller;

/**
 * @author KevinBai 446860339@qq.com
 *
 */
class JobsController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        session('menu', "jobs");
    }

    /**
     * 招聘职位列表
     */
    public function index()
    {
        check_privilege();
        $is_publish = I("get.is_publish", 1);
        $jobs_list = M("Jobs")->where("is_publish=" . $is_publish)->order("sort_order desc")->select();
        $data = array(
            "jobs_list" => $jobs_list,
            "is_publish" => $is_publish
        );
        $this->assign($data);
        $this->display("jobs_list");
    }

    /**
     * 跳转到职位添加页面
     */
    public function add_job()
    {
        check_privilege();
        $this->display("job_add");
    }

    /**
     * 执行新闻添加操作
     */
    public function do_add()
    {
        $job_name = I("post.job_name", "");
        $num = I("post.num", 0);
        $sort_order = I("post.sort_order", 0);
        $job_intro = I("post.job_intro", "", false);
        $News = M("Jobs");
        $data = array(
            "job_name" => $job_name,
            "num" => $num,
            "job_intro" => $job_intro,
            "sort_order" => $sort_order,
            "add_time" => time()
        );
        $res = $News->data($data)->add();
        echo $res;
    }

    /**
     * 跳转到修改页面
     */
    public function edit_job()
    {
        check_privilege();
        $job_id = I("get.id", 0);
        $job_info = M("Jobs")->where("id=" . $job_id)->find();
        $data = array(
            "job_info" => $job_info,
        );
        $this->assign($data);
        $this->display("job_edit");
    }

    /**
     * 执行职位修改操作
     */
    public function do_edit()
    {
        $job_id = I("post.job_id", 0);
        $job_name = I("post.job_name", "");
        $num = I("post.num", 0);
        $sort_order = I("post.sort_order", 0);
        $job_intro = I("post.job_intro", "", false);
        $News = M("Jobs");
        $data = array(
            "job_name" => $job_name,
            "num" => $num,
            "job_intro" => $job_intro,
            "sort_order" => $sort_order,
            "add_time" => time()
        );
        $res = $News->where("id=" . $job_id)->save($data);
        echo $res;
    }

    /**
     * 执行职位删除操作
     */
    public function do_delete()
    {
        $id = I("post.id", 0);
        $res = M("Jobs")->where("id=$id")->delete();
        echo $res;
    }

    //上下架
    public function on_publish()
    {
        $job_id = I("post.job_id");
        if (empty($job_id)) {
            echo json_encode(array('status' => -1, 'message' => '参数错误'));
            exit;
        }
        $status = 2;
        $job_info = M("Jobs")->where("id=" . $job_id)->find();
        if ($job_info['is_publish'] == 2) {
            $status = 1;
            //$this->add_log($this->login_user['uid'],'course_op',9,"课程上架 {$row['course_name']}");//日志
        } else {
            //下架需要删除购物车中课程
            //$this->add_log($this->login_user['uid'],'course_op',8,"课程下架 {$row['course_name']}");//日志
        }
        M("Jobs")->where("id=" . $job_id)->save(array('is_publish' => $status));
        echo json_encode(array('status' => 1));
        exit;
    }

}